package com.example.app11_gridview;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.app11_gridview.adapter.AppInfo;
import com.example.app11_gridview.adapter.MainAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemLongClickListener {

    private static final String TAG = "MainActivity";

    private Context mContext = this;

    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        Log.i(TAG, "onCreate()");

        // 初始化数据
        List<AppInfo> data = getAppInfoList();

        // 初始化控件
        GridView gvMain = findViewById(R.id.gv_main);
        gvMain.setAdapter(new MainAdapter(this, data));

        // 给GridViewItem设置点击监听
        gvMain.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String name = data.get(position).getName();
                Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
            }
        });

        // 给GridViewItem设置长按点击监听
        gvMain.setOnItemLongClickListener(this);
    }

    private List<AppInfo> getAppInfoList() {
        List<AppInfo> data = new ArrayList<>(10);
        String[] names = {"手机防盗", "通讯卫士", "软件管理", "流量管理", "进程管理", "手机杀毒",
                "缓存清理", "高级工具", "设置中心"};
        for (String name : names) {
            data.add(new AppInfo(R.drawable.ic_launcher_background, name));
        }
        return data;
    }

    /**
     * GridViewItem 长按监听事件
     * @param parent The AbsListView where the click happened
     * @param view The view within the AbsListView that was clicked
     * @param position The position of the view in the list
     * @param id The row id of the item that was clicked
     *
     * @return 返回true 表示阻止事件冒泡
     */
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        // 得到当前显示的名称
        TextView tvName = view.findViewById(R.id.tv_item_main_name);
        String appName = tvName.getText().toString();

        // 创建输入框对象
        EditText editText = new EditText(this);
        editText.setHint(appName);

        // 只修改第一个名称
        if (position == 0) {
            // 显示 AlertDialog
            new AlertDialog.Builder(this)
                    .setTitle("修改名称")
                    .setView(editText)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String newName = editText.getText().toString();
                            if (TextUtils.isEmpty(newName)) {
                                Toast.makeText(MainActivity.this, "请输入应用名称！", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            // 界面更新
                            tvName.setText(newName);
                            // 将名称保存在SharedPreference中
                            sp = mContext.getSharedPreferences("app", Context.MODE_PRIVATE);
                            SharedPreferences.Editor editor = sp.edit();
                            editor.putString("appName", newName);
                            editor.apply();
                        }
                    })
                    .setNegativeButton("取消", null)
                    .show();
        }
        return true;
    }
}